$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'aliveprice/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'Id', width: 50, key: true },
			{ label: '牧民售卖价格', name: 'mprice', index: 'mprice', width: 80 }, 			
			{ label: '贩运人收购价格', name: 'fprice', index: 'fprice', width: 80 }, 			
			{ label: '屠宰场入厂价格', name: 'trprice', index: 'trprice', width: 80 }, 			
			{ label: '时间', name: 'ptime', index: 'ptime', width: 80 }, 			
			{ label: '地区', name: 'area', index: 'area', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		starttime:'',
		endtime:'',
		provence:'',
		aliveprice: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		timequery: function (event) {
			//input.focus();
			//alert(vm.starttime);
			 $("#jqGrid").jqGrid('setGridParam',{
				 url:baseURL + 'aliveprice/timelist',
				 postData:{
					 'starttime':vm.starttime,
					 'endtime':vm.endtime,
					 'provence':vm.provence
				 },
				 datattype:'json',
				 mtyp:'GET',
				 viewrecords: true,
			     height: 385,
			     rowNum: 10,
			     rowList : [10,30,50],
			     rownumbers: true, 
			     rownumWidth: 25, 
			     autowidth:true,
			     multiselect: true,
			     pager: "#jqGridPager",
			     jsonReader : {
			         root: "page.list",
			         page: "page.currPage",
			         total: "page.totalPage",
			         records: "page.totalCount"
			     },
			     prmNames : {
			         page:"page", 
			         rows:"limit", 
			         order: "order"
			     },
			 }).trigger('reloadGrid');

	
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.aliveprice = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.aliveprice.id == null ? "aliveprice/save" : "aliveprice/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.aliveprice),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "aliveprice/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "aliveprice/info/"+id, function(r){
                vm.aliveprice = r.aliveprice;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});